package the.broccoli.uc.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wisteria.framework.web.APIResult;
import the.broccoli.auth.UserContext;
import the.broccoli.uc.controller.req.BuyAppReq;
import the.broccoli.uc.controller.req.TenantAddReq;
import the.broccoli.uc.controller.req.UpdateAppReq;
import the.broccoli.uc.dto.BuyAppDTO;
import the.broccoli.uc.dto.TenantAddDTO;
import the.broccoli.uc.service.ITenantService;

import javax.annotation.Resource;

@RestController
@RequestMapping("/uc/tenant")
public class TenantController {

    @Resource
    private ITenantService tenantService;

    @PostMapping("/add")
    public APIResult<Boolean> add(@RequestBody TenantAddReq req) {
        TenantAddDTO dto = new TenantAddDTO();
        dto.setTenantName(req.getTenantName());
        dto.setAccount(req.getAccount());
        dto.setPassword(req.getPassword());
        dto.setUserName(req.getUserName());
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(tenantService.add(dto));
    }

    /**
     * 租户购买应用
     */
    @PostMapping("/buy-app")
    public APIResult<Boolean> buyApp(@RequestBody BuyAppReq req) {
        BuyAppDTO dto = new BuyAppDTO();
        dto.setTenantId(req.getTenantId());
        dto.setAppIdList(req.getAppIdList());
        dto.setOperatorId(UserContext.userId());
        dto.setOperatorName(UserContext.userName());
        return APIResult.success(tenantService.buyApp(dto));
    }

    /**
     * 更新租户的应用信息
     * @param req   请求数据
     * @return  请求结果
     */
    @PostMapping("/update-app")
    public APIResult<Boolean> updateApp(@RequestBody UpdateAppReq req) {
        return APIResult.success(tenantService.updateApp(req));
    }
}
